package distributedlock.controller;

import distributedlock.lock.RedisLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;

/**
 * 测试类
 *
 * @author zhanghui145
 * @date 2022/2/11 14:17
 * @since 1.0.0
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private RedisLock redisLock;

    int x = 1;

    @GetMapping("/start")
    public void robLock() {
        addNum();
    }

    public void addNum() {
        redisLock.lock();
        try {
            x = x + 1;
            System.out.println(redisLock.getLockName() + "完成操作，结果：" + x);
        } finally {
            redisLock.unlock();
        }
    }
}
